package com.tsf.demo.tools.service;

import com.tencent.cloud.task.sdk.core.utils.DateUtil;
import org.springframework.cloud.client.ServiceInstance;

import java.util.*;

/**
 * <p><em>Created on 2022/3/18 10:46 下午</em></p>
 *
 * @author fuzhongtang
 * @since 1.0.1
 */
public class Register {

    public static Map<String,Long> mapServerList = new HashMap<>();

    public static List<String> listMessage = new ArrayList<>();
    public static List<String> listLog = new ArrayList<>();
    public static String testName = ""; //serviceId的前缀

    public static void register(ServiceInstance serviceInstance)
    {
        //Jmeter把时间戳放在serviceId里
        //testName1647662249518
        String serviceId = serviceInstance.getServiceId();
        String createTime_ = serviceId.replaceAll(testName,"");
        long createTime = Long.parseLong(createTime_);

        if (!mapServerList.containsKey(serviceId))
        {
            //不知道为何,手动注册的getMetadata是空的
            /*if (serviceInstance.getMetadata() != null && serviceInstance.getMetadata().get("TSF_CREATION_TIME")!=null)
            {
                createTime = Long.parseLong(serviceInstance.getMetadata().get("TSF_CREATION_TIME"));
                createTime = createTime * 1000;
            }*/

            long currentTime = System.currentTimeMillis() ;
            long usedTime = currentTime - createTime ;
            mapServerList.put(serviceId,usedTime);
            listMessage.add(serviceId+" 延时 "+usedTime +" ms, [注册时间"+ DateUtil.getDateStr(createTime)+", 同步时间" + DateUtil.getDateStr(currentTime)+"]");
        }
    }
}
